#include <QDebug>
#include "mapsearchnode.h"
#include "engine.h"


#define ABS(a) (a) > 0 ? (a) : -(a)

//MapSearchNode::MapSearchNode(Engine* engine)
//    : m_engine(engine) {
//    m_row = 0;
//    m_column = 0;
//}

MapSearchNode::MapSearchNode(Engine* engine,
                             int row,
                             int column)
    : m_engine(engine) {
    m_row = row;
    m_column = column;
}


bool MapSearchNode::operator==(const AStarSearchNode& other) const {
    const MapSearchNode* otherNode = static_cast<const MapSearchNode*>(&other);
    if (otherNode) {
//        qDebug()<<"operator==, this.row/column"<<m_row<<m_column
//               <<"other.row/column"<<otherNode->row()<<otherNode->column();
        if ((m_row == otherNode->row()) &&
                (m_column == otherNode->column())) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

float MapSearchNode::heuristicEstimateOfDistance(const AStarSearchNode& other) const {
    return distanceTo(other);
}

float MapSearchNode::distanceTo(const AStarSearchNode& other) const {
    const MapSearchNode* otherNode = static_cast<const MapSearchNode*>(&other);
    if (otherNode) {
        return ABS(m_row - otherNode->row()) + ABS(m_column - otherNode->column());
    }
    else {
        qCritical("Could not cast input node to MapSearchNode");
        return -1;
    }
}

void MapSearchNode::neighborNodes(QList<AStarSearchNode*>& neighbors) const {
    m_engine->neighborNodes(*this, neighbors);
}

int MapSearchNode::column() const {
    return m_column;
}

int MapSearchNode::row() const {
    return m_row;
}

QString MapSearchNode::nodeInfo() const {
   return QString("0x%1(%2,%3)").arg((uint)this, 0, 16).arg(m_row).arg(m_column);
}


